<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true">
      <el-form-item style="width: 150px">
        <el-input v-model="searchMap.data.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item style="width: 150px">
        <el-input
          v-model="searchMap.data.phone"
          placeholder="手机号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="block">
          <span class="demonstration"></span>
          <el-date-picker
            v-model="searchMap.data.searchTime"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="searchBtn()"
          >查询</el-button
        >
        <el-button
          icon="el-icon-circle-plus-outline"
          type="success"
          @click="addUser()"
          >添加顾客</el-button
        >
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="delUsers()"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 顾客信息列表表格 -->
    <el-table :data="userLsit" style="width: 100%" ref="userTable">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="sex" label="性别"> </el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="phone" label="手机号码"></el-table-column>
      <el-table-column
        prop="address"
        label="家庭地址"
        width="350"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        label="注册时间"
        :formatter="formatTime"
      ></el-table-column>
      <el-table-column label="操作" width="260">
        <template slot-scope="scope">
          <!-- 自定义展示 -->
          <div>
            <el-tooltip content="查看顾客配镜记录" placement="top" :enterable="false">
              <el-button type="primary" size="mini" round @click="showInfo(scope.row)" :value="scope.row" icon="el-icon-search"></el-button>
            </el-tooltip>
            <el-tooltip content="添加配镜记录" placement="top" :enterable="false">
              <el-button type="success" size="mini" icon="el-icon-circle-plus-outline" circle @click="addUserInfo(scope.row)" :value="scope.row"></el-button>
            </el-tooltip>
            <el-tooltip content="修改顾客信息" placement="top" :enterable="false">
              <el-button type="warning" size="mini" icon="el-icon-edit" circle :value="scope.row" @click="updateUser(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除顾客信息" placement="top" :enterable="false">
              <el-button type="danger" size="mini" icon="el-icon-delete" circle :value="scope.row" @click="delUser(scope.row)"></el-button>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <el-pagination
      style="text-align: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 顾客配镜信息弹出层 编号1 -->
    <el-dialog
      :title="'顾客配镜信息'"
      :visible.sync="dialogFormVisible1"
      :close-on-click-modal="false"
      width="85%"
    >
      <el-table :data="userInfoList" style="width: 100%" border>
      <el-table-column prop="createTime" label="日期" width="160" :formatter="formatTime"></el-table-column>
      <el-table-column prop="glassesType" label="视别" width="60">
        <template slot-scope="scope">
          {{
            scope.row.glassesType == 1 ? "近用"
              :scope.row.glassesType == 2 ? "远用" : "其他"
          }}
        </template>
      </el-table-column>
      <el-table-column prop="eyeglassFrames" label="眼镜架"></el-table-column>
      <el-table-column prop="specteclesGlasses" label="镜片"></el-table-column>
      <el-table-column prop="eyeballRightQiujing" label="右-球镜" width="75"></el-table-column>
      <el-table-column prop="eyeballLeftQiujing" label="左-球镜" width="75"></el-table-column>
      <el-table-column prop="eyeballRightZhujing" label="右-柱镜" width="75"></el-table-column>
      <el-table-column prop="eyeballLeftZhujing" label="左-柱镜" width="75"></el-table-column>
      <el-table-column prop="eyeballRightZhouwei" label="右-轴位" width="75"></el-table-column>
      <el-table-column prop="eyeballLeftZhouwei" label="左-轴位" width="75"></el-table-column>
      <el-table-column prop="eyeballRightTongju" label="右-瞳距" width="75"></el-table-column>
      <el-table-column prop="eyeballLeftTongju" label="左-瞳距" width="75"></el-table-column>
      <el-table-column prop="price" label="实收金额" width="80">
        <template slot-scope="scope">
          {{scope.row.price?(scope.row.price+'￥'):''}}
        </template>
      </el-table-column>
      <el-table-column prop="info" label="备注" width="100"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="delUserInfo(scope.row)"
            type="text"
            size="small">
            移除
          </el-button>
          <el-button
            @click.native.prevent="updateUserInfo(scope.row)"
            type="text"
            size="small">
            修改
          </el-button>
        </template>
      </el-table-column>
      </el-table>
      <div style="margin-top: 10px;margin-right:120px;float:right">金额总计：{{priceTotal}}元</div>
    </el-dialog>

    <!-- 顾客信息弹出层 编号2-->
    <el-dialog
        :title="userEntity.id?'修改顾客信息':'添加顾客'"
        :visible.sync="dialogFormVisible2"
        :close-on-click-modal="false"
        >
        <el-form :model="userEntity"  ref="userForm" :rules="userRules">
            <el-form-item label="姓名" label-width="100px" prop="name">
            <el-input v-model="userEntity.name"  style="color:black" placeholder="顾客姓名 必填！！"></el-input>
            </el-form-item>
            <el-form-item label="性别" label-width="100px">
            <el-input v-model="userEntity.sex" style="color:black"></el-input>
            </el-form-item>
            <el-form-item label="年龄"  label-width="100px">
            <el-input v-model="userEntity.age" style="color:black"></el-input>
            </el-form-item>
            <el-form-item label="手机号码"  label-width="100px" prop="phone">
            <el-input v-model="userEntity.phone" style="color:black" placeholder="顾客手机号码 必填！！"></el-input>
            </el-form-item>
            <el-form-item label="家庭地址"  label-width="100px">
            <el-input v-model="userEntity.address" style="color:black"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="submitUserForm('userForm')">确 定</el-button>
        </div>
        </el-dialog>

        <!-- 顾客配镜信息表单弹出层 编号3 -->
    <el-dialog
      :title="userinfoEntity.id?'修改配镜信息':'添加顾客配镜信息'"
      :visible.sync="dialogFormVisible3"
      :close-on-click-modal="false"
      width="35%"
      top="11vh"
    >
      <el-form :model="userinfoEntity"  ref="userinfoForm" :rules="userinfoRules">
            <el-form-item label="视别:" label-width="100px" prop="glassesType">
              <el-radio-group v-model="userinfoEntity.glassesType">
              <el-radio :label="'1'">近用</el-radio>
              <el-radio :label="'2'">远用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="眼镜架:" label-width="100px">
            <el-input v-model="userinfoEntity.eyeglassFrames" style="color:black;width:350px"></el-input>
            </el-form-item>
            <el-form-item label="镜片:"  label-width="100px">
            <el-input v-model="userinfoEntity.specteclesGlasses" style="color:black;width:350px"></el-input>
            </el-form-item>
            <el-form-item label="球镜:"  label-width="100px">
            <span style="margin-left: 15px;">右眼：</span><el-input v-model="userinfoEntity.eyeballRightQiujing" style="color:black;width:100px"></el-input>
            <span style="margin-left: 15px;">左眼：</span><el-input v-model="userinfoEntity.eyeballLeftQiujing" style="color:black;width:100px"></el-input>
            </el-form-item>
            <el-form-item label="柱镜:"  label-width="100px">
            <span style="margin-left: 15px;">右眼：</span><el-input v-model="userinfoEntity.eyeballRightZhujing" style="color:black;width:100px"></el-input>
            <span style="margin-left: 15px;">左眼：</span><el-input v-model="userinfoEntity.eyeballLeftZhujing" style="color:black;width:100px"></el-input>
            </el-form-item>
            <el-form-item label="轴位:"  label-width="100px">
            <span style="margin-left: 15px;">右眼：</span><el-input v-model="userinfoEntity.eyeballRightZhouwei" style="color:black;width:100px"></el-input>
            <span style="margin-left: 15px;">左眼：</span><el-input v-model="userinfoEntity.eyeballLeftZhouwei" style="color:black;width:100px"></el-input>
            </el-form-item>
            <el-form-item label="瞳距:"  label-width="100px">
            <span style="margin-left: 15px;">右眼：</span><el-input v-model="userinfoEntity.eyeballRightTongju" style="color:black;width:100px"></el-input>
            <span style="margin-left: 15px;">左眼：</span><el-input v-model="userinfoEntity.eyeballLeftTongju" style="color:black;width:100px"></el-input>
            </el-form-item>
            <el-form-item label="实收金额:" label-width="100px" prop="price">
            <el-input v-model="userinfoEntity.price" style="color:black;width:100px;"></el-input><span>元</span>
            </el-form-item>
            <el-form-item label="备注:" label-width="100px">
            <el-input v-model="userinfoEntity.info" style="color:black;width:350px;"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible3 = false">取 消</el-button>
            <el-button type="primary" @click="submitUserinfoForm('userinfoForm')">确 定</el-button>
        </div>
    </el-dialog>

  </div>
</template>


<script>
import { listSearch,userSave,delById,delByIds } from "@/api/userlist.js";
import { getById,userInfoSave,userInfoDelById } from "@/api/userinfo.js";
import {isvalidPhone} from '@/api/validate.js'
 var validPhone=(rule, value,callback)=>{
      if (!value){
          callback(new Error('请输入电话号码'))
      }else  if (!isvalidPhone(value)){
        callback(new Error('请输入正确的11位手机号码'))
      }else {
          callback()
      }
  }
export default {
  created() {
    this.search();
  },
  data() {
    return {
      searchMap: {
        page: 1,
        limit: 10,
        data: {},
      },
      userLsit: [],
      dialogFormVisible1: false,
      dialogFormVisible2:false,
      dialogFormVisible3:false,
      total: 0,
      //顾客表单校验
      userRules: {
        name:[
          { required: true, message: '请输入顾客姓名', trigger: 'blur' },
        ],
        phone:[
          { required: true, message: '请输入顾客电话号码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validPhone }
        ]
      },
      //顾客配镜信息表单校验
      userinfoRules: {
        price:[
          { pattern: /^-?\d+\.?\d*$/, message: '请输入纯数字', trigger: 'blur'}
        ]
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      userInfoList:[],
      priceTotal:0,
      userinfoEntity: {},
      userEntity:{},
      userId:0,
    };
  },
  methods: {
    searchBtn(){
      this.searchMap.page=1;
      this.search();
    },
    search() {
      listSearch(this.searchMap).then((res) => {
        this.userLsit = res.data;
        this.total = res.total;
      });
    },
    handleSizeChange(pageSize) {
      //改变每页数据显示条数触发
      this.searchMap.limit = pageSize;
      this.search();
    },
    handleCurrentChange(currentPage) {
      //查看指定页数事件触发
      this.searchMap.page = currentPage;
      this.search();
    },
    showInfo(row) {
      this.dialogFormVisible1 = true;
      this.userId = row.id;
      var _this = this;
      _this.priceTotal = 0;
      getById(row.id).then(res=>{
        this.userInfoList=res;
        res.forEach(function(element) {
          _this.priceTotal += element.price;
        });
      });
    },

    //添加顾客信息按钮触发事件
    addUser(){
      this.dialogFormVisible2 = true;
      this.userEntity = {};
    },
    //修改顾客信息按钮触发事件
    updateUser(row){
      this.dialogFormVisible2 = true;
      this.userEntity = JSON.parse(JSON.stringify(row));
    },
    //添加保存与修改保存
    saveUser(){
      userSave(this.userEntity).then(res=>{
        if(res.success){
          this.dialogFormVisible2 = false;
          this.$message({
            message: res.msg,
            type: "success",
            center: true,
          });
          this.search();
        }else{
          this.$message({
            message: res.msg,
            type: "error",
            center: true,
          });
        }
      });
    },

    //顾客信息行删除
    delUser(row){
      this.$confirm("此操作将删除该行记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          delById(row.id).then((res) => {
            if (res.success) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.search();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //顾客信息批量删除
    delUsers(){
      var rowDatas = this.$refs.userTable.selection;
      var ids=[];
      
      this.$confirm("此操作将删除选中的多条记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          rowDatas.forEach(element => {
             ids.push(element.id);
          });
          delByIds(ids).then((res) => {
            if (res.success) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.search();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //顾客信息表单submit按钮
    submitUserForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.saveUser();
          } else {
            this.$message({
                type: "error",
                message: "请按要求填写顾客信息！！",
              });
            return false;
          }
        });
    },
    //顾客信息表单submit按钮
    submitUserinfoForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.saveUserInfo();
          } else {
            this.$message({
                type: "error",
                message: "请按要求填写顾客配镜信息！！",
              });
            return false;
          }
        });
    },
    //顾客配镜信息添加
    addUserInfo(row){
      this.userId = row.id;
      this.dialogFormVisible3 = true;
      this.userinfoEntity = {};
    },
    saveUserInfo(){
      console.log(this.userId)
      console.log(this.userinfoEntity);
      this.userinfoEntity.userId=this.userId;
      userInfoSave(this.userinfoEntity).then(res=>{
        if(res.success){
          this.dialogFormVisible3 = false;
          this.dialogFormVisible1 = false;
          this.$message({
            message: res.msg,
            type: "success",
            center: true,
          });
          this.search();
        }else{
          this.$message({
            message: res.msg,
            type: "error",
            center: true,
          });
        }
      });
    },
    //修改顾客配镜信息
    updateUserInfo(row){
      this.dialogFormVisible3 = true;
      this.userinfoEntity = JSON.parse(JSON.stringify(row));
    },
    //删除顾客配镜信息
    delUserInfo(row){
      this.$confirm("此操作将删除该条记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          userInfoDelById(row.id).then((res) => {
            if (res.success) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.dialogFormVisible1=false;
              this.dialogFormVisible3=false;
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },








    formatTime(row, column) {
      const updateTime = new Date(row[column.property]);
      return (
        updateTime.getFullYear() +
        "年" +
        updateTime.getMonth() +
        "月" +
        updateTime.getDate() +
        "日 " +
        updateTime.getHours() +
        ":" +
        updateTime.getMinutes()
      );
    },
  },
};
</script>
<style lang="scss">
.goodsStatus {
  background-color: rgb(51, 171, 160);
}
</style>